#ifndef _BLUETOOTH_APP_H_
#define _BLUETOOTH_APP_H_

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

// How often to perform periodic event
#define SBP_PERIODIC_EVT_PERIOD                             (1600)

// How often to perform read rssi event
#define SBP_READ_RSSI_EVT_PERIOD                            (3200)

// Parameter update delay
#define SBP_PARAM_UPDATE_DELAY                              (6400)

// Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely
#define DEFAULT_DISCOVERABLE_MODE                           GAP_ADTYPE_FLAGS_GENERAL

// Minimum connection interval (units of 1.25ms, 6=7.5ms)
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL                   6

// Maximum connection interval (units of 1.25ms, 100=125ms)
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL                   100

// Slave latency to use parameter update
#define DEFAULT_DESIRED_SLAVE_LATENCY                       (0)

// Supervision timeout value (units of 10ms, 100=1s)
#define DEFAULT_DESIRED_CONN_TIMEOUT                        (300ul)

// 默认判断接收完毕时间 - 1s
#define DEFAULT_BLUETOOTH_RECIVE_TIMEOUT                    (1000ul)
// 默认蓝牙发送延时 - 60ms
#define DEFAULT_BLUETOOTH_TRANSMIT_DELAY                    (60ul)

// 报文长度
#define BLUETOOTH_MESSAGE_PACKET_LENGTH                     (NOMINAL_PACKET_LENGTH)
// 重启更新蓝牙广播包时间 - 5s
#define BLUETOOTH_UPDATE_RADIO_PACKAGE_TIME                 (5000ul)

/**
 * @brief 从机事件任务
 * 
 */
typedef enum
{
  BLUETOOTH_START_DEVICE_EVENT                   = 1 << 0, // 启动
  BLUETOOTH_READ_RSSI_EVENT                      = 1 << 2, // 读取信号强度
  BLUETOOTH_PARAM_UPDATE_EVENT                   = 1 << 3, // 更新传输
  BLUETOOTH_PHY_UPDATE_EVENT                     = 1 << 4, // phy 更新
  BLUETOOTH_RECIVE_TIMEOUT_EVENT                 = 1 << 5, // 接收数据超时
  BLUETOOTH_CONNECT_TIMEOUT_EVENT                = 1 << 6, // 连接超时
  BLUETOOTH_SEND_DATA_TO_HOST_EVENT              = 1 << 7, // 向主机发送数据
  BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT = 1 << 8, // 更新广播包
} PeripheralTaskEvent_t;

/**
 * @brief 
 * 
 */
typedef struct
{
  uint16_t ConnHandle;       // 
  uint16_t ConnTimeout;      // 
  uint16_t ConnInterval;     // 
  uint16_t ConnSlaveLatency; // 
} PeripheralConnItem_t;

/**
 * @brief 蓝牙收发数据堆栈
 *
 */
typedef struct
{
  uint16_t DataSize;                       // 数据长度
  uint16_t LengthOfTransmit;               // 已发送数据长度
  uint8_t DataBuff[NOMINAL_PACKET_LENGTH]; // 数据
} BluetoothDataBuff_t;

/**
 * @brief 更新广播包
 *
 */
extern void BluetoothUpdateRadioPackage(void);

/**
 * @brief 断开当前蓝牙连接
 *
 */
extern void BluetoothDisconnectFromTheHost(void);

/**
 * @brief 向主机发送一定长度的数据
 *
 * @param pDataIn  - 待发送的数据
 * @param DataSize - 发送数据有效长度
 * @return 发送状态（true：成功 false：失败）
 */
extern bool BluetoothSendDataToHost(const void *pDataIn, uint16_t DataSize);

/**
 * @brief 初始化蓝牙相关功能
 *
 */
extern void BluetoothPeripheralInit(void);

/**
 * @brief 打开蓝牙从机任务
 * 
 */
extern void start_peripheral_task(void);

#endif
